// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index'
// import axios from 'axios'
import wx from 'weixin-js-sdk'
import MintUi from 'mint-ui'
import 'mint-ui/lib/style.css'
import 'normalize.css'
import 'assets/css/common.css'
import 'assets/font/iconfont.css'
import 'assets/js/rem.js'
import ls from 'assets/js/LStorage.js'
import axios from 'assets/js/config.js'
import http from 'axios'

Vue.config.productionTip = false;
Vue.prototype.axios = axios;
Vue.prototype.http = http;
Vue.prototype.ls = ls;
Vue.prototype.wx = wx;
Vue.use(MintUi);
Vue.prototype.changeTime = function (nS) {
	var date = new Date();
    date.setTime(nS);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
	// return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
	return y + '-' + m + '-' + d
};

Vue.prototype.getLocalTime = function (nS) {  //时间戳转换  xx-xx-xx
    var date = new Date();
    date.setTime(nS);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d
}
Vue.directive('title', {
    inserted: function (el, binding) {
     document.title = binding.value
    }
})
// router.beforeEach( (to, from, next) => {
    // axios.get(store.state.hostname + 'yh/getyhinfo.do')
    //     .then( res => {
    //         console.log(res)
    //         // if (true) {}
    //         store.state.wrap = true;
    //         store.state.info = true;
    //         store.state.photo = false;
    //         console.log(store.state.info)
    //         // console.log(to, from)
    //         if (to.path === '/personInfo') {
    //             next()
    //         }
    //     }).catch( res => {
    //         console.log(res)
    //     })
    // next()
// })

/* eslint-disable no-new */
new Vue({
  	el: '#app',
  	router,
  	store,
    created () {
			// alert(this.ls)
        // console.log(this.$router)
				// console.log(1)
				// alert(this.ls.getItem("userID"),6688)
        if (this.ls.getItem("userID")) {
            this.http.get(store.state.hostname + 'yh/getyhinfo.do',{
                date: new Date()
            }).then( res => {
                    store.state.apiBol = true;
                    this.wxapi()
                    store.state.person = res.data.data;
                    if (!res) return;
                    if (!res.data.data.id) MintUi.MessageBox.alert('信息获取失败');

                    if (!res.data.data.xm || !res.data.data.sjh) {
                        store.state.wrap = true;
                        store.state.info = true;
                        store.state.photo = false;
                    }else{
                        store.state.wrap = true;
                        store.state.info = false;
                        store.state.photo = true;
                    }
                    if (this.$route.name === 'aboutme' || this.$route.name ===  'personInfo' || this.$route.name ===  'upload-pick' || this.$route.name ===  'upload-lose') {
                        store.state.wrap = false;
                    }
										this.ls.setItem('userID', res.data.data.id);
                    MintUi.Indicator.close();
                }).catch( res => {
                    MintUi.Indicator.close();
                })
        }else{
            this.wxapi()
        }

    },
    methods: {
        wxapi () {
            var self = this;
            var url = window.location.href.split("#")[0];
            this.http.get(this.$store.state.hostname + 'yh/getsign.do',{
                params: {
                    date: new Date(),
                    url: url
                }
            }).then( res => {
                console.log(res)
								console.log(9990909)
                var res = res.data.data;
                MintUi.Indicator.close();
                this.wx.config({
                    appId: self.$store.state.appId,
                    timestamp: res.timestamp,
                    nonceStr: res.nonceStr,
                    signature: res.signature,
                    jsApiList: [
                        'getLocation',
                        'openLocation',
                        'chooseImage'
                    ]
                })

                // this.wx.ready(function(){
                //     self.wx.getLocation({
                //         type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                //         success: function (res) {
                //             var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                //             var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                //             alert(latitude + "," + longitude + "," + speed + "," + accuracy);
                //             self.wd = latitude;
                //             self.jd = longitude;
                //             // console.log("plae---------")
                //             // console.log(res)
                //             // console.log(latitude, longitude)
                //         },
                //         fail: function (res) {
                //             alert(JSON.stringify(res))
                //             console.log(res)
                //         }
                //     })
                // })
            })
        }
    },
  	template: '<App/>',
  	components: { App }
})
